﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ITJZ.BitHelper.APIModel;
using ITJZ.BitHelper.IAPI;
using System.Xml;
using System.Data.OleDb;
using System.Collections.Generic;

namespace ITJZ.BitHelper.Web.API
{

    public  class Knowledge:IKnowledge
    {
        #region IKnowledge 成员

        public System.Xml.XmlDocument Menu()
        {


            XmlDocument doc = new XmlDocument();
            doc.LoadXml(
                @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<menu>
  <node title=""c#"" value=""(c_sharp)"">
    <node title=""顶级函数"" value=""(0)""/>    
    <node title=""框架集"" value=""(dotnet_framework)""/>
  </node>
  <node title=""javascript"" value=""(javascript)"" >
    <node title=""顶级函数"" value=""(0)""></node>
    <node title=""jquery"" value=""(jquery)""></node>
    <node title=""extjs"" value=""(extjs)""></node>
  </node>
  <node title=""java"" value=""(java)"">
    <node title=""顶级函数"" value=""(0)""/>
    <node title=""android"" value=""(android)""/>
  </node>
  
  <node title=""asp"" value=""(asp)"">
    <node title=""顶级函数"" value=""(0)""/>
  </node>
    
  <node title=""php"" value=""(php)"">
    <node title=""顶级函数"" value=""(0)""/>
    <node title=""smarty"" value=""(smarty)""/>
  </node>
  <node title=""jsp"" value=""(jsp)"">
    <node title=""顶级函数"" value=""(0)""/>
  </node>
</menu>");

            return doc;
        }

        public System.Xml.XmlDocument LibList()
        {
            throw new NotImplementedException();
        }

        public ResInfo Add(string language, string library, string fullName, string content)
        {

            //string uid = HttpContext.Current.Session["uid"] as string;

            //if (string.IsNullOrEmpty(uid))
            //{
            //    resInfo.ErrState = 1;
            //    resInfo.Msg = "未登录";
            //    return resInfo;//未登录直接返回
            //} 

            APIModel.User user = User.GetUser();

            string uid = user.Uid;


            ResInfo resInfo = new ResInfo();

            using (OleDbConnection  conn=new  OleDbConnection(Config.connStr))
            {
                string sql = @"INSERT INTO Knowledge
      ([guid], uid, [language], library, fullName, content, showTime, vote, createTime)
VALUES (@guid,@uid,@language,@library,@fullName,@content,@showTime,@vote,now())"; 

                using (OleDbCommand cmd=new OleDbCommand (sql,conn))
                {

                    cmd.Parameters.AddWithValue("@guid", Guid.NewGuid().ToString());
                    cmd.Parameters.AddWithValue("@uid",uid);
                    cmd.Parameters.AddWithValue("@language",language);

                    
                    cmd.Parameters.AddWithValue("@library",library);
                    cmd.Parameters.AddWithValue("@fullName",fullName);
                    cmd.Parameters.AddWithValue("@content",content);

                    
                    cmd.Parameters.AddWithValue("@showTime",0);
                    cmd.Parameters.AddWithValue("@vote",0);


                    conn.Open();
                    int ret=  cmd.ExecuteNonQuery();

                    if (ret>0)
                    {
                        resInfo.ErrState = 0;
                        resInfo.Msg = "添加成功";
                    }
                    else
                    {
                        resInfo.ErrState = 1;
                        resInfo.Msg = "新建失败，原因未知";
                    }
                }
            }

            return resInfo;
        }

        public List<ITJZ.BitHelper.APIModel.Knowledge> Search(string language, string library, string keywords)
        {

            List<ITJZ.BitHelper.APIModel.Knowledge> res = new List<ITJZ.BitHelper.APIModel.Knowledge>();

            using (OleDbConnection conn=new OleDbConnection (Config.connStr))
            {
                string sql= @"
SELECT [CreateTime],[FullName],[Guid],[Language],[Library],[showTime],[uid],[vote]
FROM Knowledge
WHERE (Knowledge.[language] = @language) AND (Knowledge.library = @library) AND 
      (Knowledge.fullName LIKE @key)
";

                using (OleDbCommand cmd=new OleDbCommand (sql,conn))
                {
                    cmd.Parameters.AddWithValue("@language", language);
                    cmd.Parameters.AddWithValue("@library", library);
                    cmd.Parameters.AddWithValue("@key", keywords);
                    conn.Open();
                    using( OleDbDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            ITJZ.BitHelper.APIModel.Knowledge temp = new ITJZ.BitHelper.APIModel.Knowledge();
                            //temp.Content = (string)reader["Content"];
                            temp.CreateTime = (DateTime)reader["CreateTime"];
                            temp.FullName = (string)reader["FullName"];
                            temp.Guid = (string)reader["Guid"];
                            temp.Language = (string)reader["Language"];
                            temp.Library = (string)reader["Library"];
                            temp.ShowTime = (int)reader["showTime"];
                            temp.Uid = (string)reader["uid"];
                            temp.Vote = (int)reader["vote"];

                            res.Add(temp);
                        }
                    }
                    
                }

//#if DEBUG
//                if (res.Count==0)
//                {
//                    ITJZ.BitHelper.APIModel.Knowledge temp = new ITJZ.BitHelper.APIModel.Knowledge();
//                    temp.Content = "这是由于没有查询到数据返回的测试内容";
//                    temp.CreateTime = DateTime.Now;
//                    temp.FullName ="仅供测试";
//                    temp.Guid = "ceshi-cceesshhii";
//                    temp.Language = "测试的语言";
//                    temp.Library = "(0)";
//                    temp.ShowTime = 99;
//                    temp.Uid = "iitt";
//                    temp.Vote = 0;

//                    res.Add(temp);

//                }
//#endif
                return res;
            }



        }

        public ITJZ.BitHelper.APIModel.Knowledge GetKnowledge(string guid)
        {
            ITJZ.BitHelper.APIModel.Knowledge res = new ITJZ.BitHelper.APIModel.Knowledge();
            using (OleDbConnection conn = new OleDbConnection(Config.connStr))
            {
                string sql = @"
SELECT Knowledge.*
FROM Knowledge
WHERE ([guid] = @guid)

";
                using (OleDbCommand cmd=new OleDbCommand (sql,conn))
                {
                    cmd.Parameters.AddWithValue("@guid",guid);
                    conn.Open();
                    using (OleDbDataReader reader=cmd.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            res.Content = (string)reader["Content"];
                            res.CreateTime = (DateTime)reader["CreateTime"];
                            res.FullName = (string)reader["FullName"];
                            res.Guid = (string)reader["Guid"];
                            res.Language = (string)reader["Language"];
                            res.Library = (string)reader["Library"];
                            res.ShowTime = (int)reader["showTime"];
                            res.Uid = (string)reader["uid"];
                            res.Vote = (int)reader["vote"];
                        }
                    }

                }
            }

            return res;
        }

        public ResInfo Vote(string knowledgeGuid, int score)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
